package zisu.netty.practice.RPC.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import zisu.netty.practice.RPC.client.codec.RpcDecode;
import zisu.netty.practice.RPC.client.codec.RpcEncode;
import zisu.netty.practice.RPC.client.codec.RpcRequest;
import zisu.netty.practice.RPC.client.codec.RpcResponse;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2021/1/12 16:05
 */
public class RpcClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {

        ChannelPipeline pipeline = ch.pipeline();
        // 编解码 的 handler
        pipeline.addLast(new RpcEncode(RpcRequest.class));
        // LengthFieldBasedFrameDecoder 这个类会大致的帮你 解析 数据包的 一个结构。
//      @param lengthFieldOffset
//              the offset of the length field      //数据包的起始位置
//      @param lengthFieldLength
//              the length of the length field      //数据包的长度位数 结束位置（这里是4 就代表 dataLength占位4个字节）
        pipeline.addLast(new LengthFieldBasedFrameDecoder(65535,0,4,0,0));
        pipeline.addLast(new RpcDecode(RpcResponse.class));
        // 实际的业务处理类。RpcClientHandler
        pipeline.addLast(new RpcClientHandler());

    }
}
